package com.douqu.game.main.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.bean.core.util.Utils;
import com.douqu.game.core.controller.PlayerController;
import com.douqu.game.core.e.ReturnMessage;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.common.TwoTuple;
import com.douqu.game.core.factory.HttpFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.util.PayUtils;
import com.douqu.game.core.web.RechargeCallBackParam;
import com.douqu.game.core.web.response.BaseResponseDto;
import com.douqu.game.core.web.response.ErrorResponseDto;
import com.douqu.game.core.web.response.SuccessResponseDto;
import com.douqu.game.main.GameServer;
import com.douqu.game.main.database.mapper.read.ReadOrderRecordMapper;
import com.douqu.game.main.database.mapper.write.WriteOrderRecordMapper;
import com.douqu.game.main.service.PlayerService;
import com.douqu.game.main.util.MsgUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
* @author wangzhenfei
*         2018-01-17 15:42
*         易接消费回调
*/
@RestController
public class PayNotifyController {
    @Autowired
    WriteOrderRecordMapper writeOrderRecordMapper;

    @Autowired
    ReadOrderRecordMapper readOrderRecordMapper;

    @Autowired
    PlayerService playerService;


    @RequestMapping(value = "/order/test")
    public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter w = response.getWriter();
        LogUtils.error("收到测试数据");
        w.write("hahahahahaha");
        w.flush();
        w.close();
    }



    @RequestMapping(value = HttpFactory.ORDER_CHECK, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public BaseResponseDto checkOrder(@RequestBody RechargeCallBackParam reqDto) throws IOException {
        if(!reqDto.checkPassword(GameServer.getInstance().getServerId())){
            return new ErrorResponseDto(ReturnMessage.PASSWORD_ERROR);
        }

        TwoTuple<Player, PlayerController> player = GameServer.getInstance().getWorldManager().getPlayerResult(reqDto.getObjectIndex());
        if(player == null || player.getFirst() == null)
        {
            LogUtils.error("充值用户找不到:" + reqDto.getObjectIndex());
            return new ErrorResponseDto(ReturnMessage.PARAM_ERROR);
        }

        LogUtils.warn("收到冲值验证 -> " + JSONObject.toJSONString(reqDto));
        if(playerService.recharge(player.getSecond(), player.getFirst(), reqDto.getInnerParam(), reqDto.getFee(), reqDto.getTcd(), reqDto.getPayType(), reqDto.getTest()==1)){
            LogUtils.warn("充值奖励发放成功");
            GameServer.getInstance().getWorldManager().verifyTradeEnd(reqDto.getOutTradeNo());
            //如果是苹果支付，需要返回verify消息
            SGCommonProto.E_RECHARGE_TYPE rechargeType = null;
            if(reqDto.getPayType().equals(PayUtils.getEnumType(SGCommonProto.E_RECHARGE_TYPE.RECHARGE_TYPE_APPSTORE.name())))
                rechargeType = SGCommonProto.E_RECHARGE_TYPE.RECHARGE_TYPE_APPSTORE;

            if(rechargeType != null && player.getSecond() != null)
                MsgUtils.sendRechargeVerify(player.getSecond(), true, reqDto.getOutTradeNo(), rechargeType, reqDto.getErrorMsg());

            return new SuccessResponseDto();
        }else {
            LogUtils.error("充值方法返回错误，配置找不到");
            return new ErrorResponseDto(ReturnMessage.PARAM_ERROR);
        }

    }


    @RequestMapping(value = HttpFactory.ORDER_ERROR_NOTIFY, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public BaseResponseDto appstoreNotify(@RequestBody RechargeCallBackParam reqDto)
    {
        GameServer.getInstance().getWorldManager().verifyTradeEnd(reqDto.getOutTradeNo());

        if(!reqDto.checkPassword(GameServer.getInstance().getServerId())){
            return new BaseResponseDto(ReturnMessage.PASSWORD_ERROR);
        }

        TwoTuple<Player, PlayerController> player = GameServer.getInstance().getWorldManager().getPlayerResult(reqDto.getObjectIndex());
        if(player == null || player.getFirst() == null)
        {
            LogUtils.error("充值用户找不到:" + reqDto.getObjectIndex());
            return new BaseResponseDto(ReturnMessage.PARAM_ERROR);
        }

        if(!Utils.isNullOrEmpty(reqDto.getErrorMsg()))
        {
            if(player.getSecond() != null)
            {
                LogUtils.error("认证服务器返回错误信息->" + reqDto.getErrorMsg());
                player.getSecond().sendWarn(reqDto.getErrorMsg());
                SGCommonProto.E_RECHARGE_TYPE rechargeType = null;
                if(reqDto.getPayType().equals(PayUtils.getEnumType(SGCommonProto.E_RECHARGE_TYPE.RECHARGE_TYPE_APPSTORE.name())))
                    rechargeType = SGCommonProto.E_RECHARGE_TYPE.RECHARGE_TYPE_APPSTORE;

                if(rechargeType != null)
                    MsgUtils.sendRechargeVerify(player.getSecond(), false, reqDto.getOutTradeNo(), rechargeType, reqDto.getErrorMsg());
            }
        }

        return new BaseResponseDto(ReturnMessage.SUCCESS);
    }





}
